package com.xm.servlet;

import com.xm.biz.CartBiz;
import com.xm.biz.FavoritesBiz;
import com.xm.biz.GoodsBiz;
import com.xm.biz.impl.CartBizImpl;
import com.xm.biz.impl.FavoritesBizImpl;
import com.xm.biz.impl.GoodsBizImpl;
import com.xm.po.Cart;
import com.xm.po.Favorites;
import com.xm.po.Goods;
import com.xm.po.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-26-10:09
 * @description 收藏处理器
 */
@WebServlet("/like")
public class FavoritesGetAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        switch (type){
            case "getAll":
                getAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "del":
                del(req, resp);
                break;
        }
    }

    /**
     * 用户收藏列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //取出用户信息
        HttpSession session = req.getSession();
        Users user = (Users) session.getAttribute("user");
        FavoritesBiz favoritesBiz = new FavoritesBizImpl();
        List<Favorites> favoritesList = favoritesBiz.getAll(user.getUserId());
        req.setAttribute("favoritesList",favoritesList);
        req.getRequestDispatcher("person/collection.jsp").forward(req,resp);
    }

    /**
     * 新增收藏商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");
        FavoritesBiz favoritesBiz = new FavoritesBizImpl();
        //获取商品id
        Integer id = Integer.valueOf(req.getParameter("goodsId"));
        //取出用户信息
        HttpSession session = req.getSession();
        Users user = (Users) session.getAttribute("user");
        Favorites check = favoritesBiz.check(id, user.getUserId());
        PrintWriter out = resp.getWriter();
        if (check==null){
            Favorites favorites = new Favorites(null,user.getUserId(),id,new Date());
            boolean result = favoritesBiz.add(favorites);
            if (result){
                out.print("true");
            }else {
                out.print("false");
            }
            out.flush();
            out.close();
        }
    }

    /**
     * 删除收藏商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        FavoritesBiz favoritesBiz = new FavoritesBizImpl();
        //获取商品id
        Integer id = Integer.valueOf(req.getParameter("goodsId"));
        //取出用户信息
        HttpSession session = req.getSession();
        Users user = (Users) session.getAttribute("user");
        Favorites check = favoritesBiz.check(id, user.getUserId());
        boolean result = favoritesBiz.deleteById(check.getFavoriteId());
        if (result){
            resp.sendRedirect("like?type=getAll");
        }
    }
}
